/*
 * This file is part of the Boring Engine.
 *
 * Copyright (c) 2011 by pf5234 <pf5234@users.sourceforge.net>.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package com.googlecode.boringengine.internal.java2drendering;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import com.googlecode.boringengine.Sprite;

public class Java2DSprite implements Sprite {

    protected BufferedImage sprite;
    private int w, h;

    public Java2DSprite(String pixels, int width, int height) {
        sprite = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
        w = width; h = height;
        Graphics2D g = sprite.createGraphics();
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int index = y * width + x;
                char ch = pixels.charAt(index);
                int pixelNum = ch - '0';
                Color color;
                int colorNum = 255 * pixelNum / 9;
                if (ch == '.') //alpha
                    color = new Color(0, 0, 0, 0);
                else
                    color = new Color(colorNum, colorNum, colorNum, 255);
                g.setColor(color);
                g.fillRect(x, y, 1, 1);
            }
        }
    }

    public Java2DSprite(BufferedImage image) {
        sprite = image;
        w = image.getWidth();
        h = image.getHeight();
    }

    public int getWidth() {
        return w;
    }

    public int getHeight() {
        return h;
    }

    BufferedImage getImage() {
        return sprite;
    }

}
